package com.zrt.pad.commonui;


import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zrt.pad.padpos.R;


public class WaitBoard implements OnClickListener, Drawable.Callback{

	public static final WaitBoard instance = new WaitBoard();

	private RelativeLayout layout;
	private TextView informationView;
	private WindowManager windowManager;
	private Handler handler;
	
	private WaitBoard(){
		LayoutInflater inflater = (LayoutInflater) SharedApplication.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layout = (RelativeLayout) inflater.inflate(R.layout.wait_board, null);
		informationView = (TextView) layout.findViewById(R.id.wait_information);
		handler = new Handler(Looper.getMainLooper());
	}
	
	
	@Override
	public void onClick(View v) {
		
	}
	
	
	public void show(Activity context){
		windowManager = context.getWindowManager();
		WindowManager.LayoutParams p = new WindowManager.LayoutParams();
		p.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
		p.format = PixelFormat.TRANSLUCENT;
		p.windowAnimations = android.R.anim.fade_in;
		windowManager.addView(layout, p);
	}
	
	public void hide(){
		setAnimateRun(false);
		windowManager.removeView(layout);
		informationView.setText("");
		windowManager = null;
	}
	
	public void setInformation(SpannableString information){
		informationView.setText(information);
		setAnimateRun(true);
	}
	
	public Spannable getInformation(){
		CharSequence cs = informationView.getText();
		if (cs instanceof Spannable) {
			return (Spannable) cs;
		}
		return new SpannableString(cs);
	}
	
	private void setAnimateRun(boolean go){
		CharSequence text = informationView.getText();
		Spanned spanned = (Spanned) text;
		ImageSpan[] spans = spanned.getSpans(0, spanned.length() - 1, ImageSpan.class);
		for (ImageSpan is : spans) {
			Drawable d = is.getDrawable();
			if (d instanceof AnimationDrawable) {
				AnimationDrawable ad = (AnimationDrawable) d;
				if (go) {
					if (!ad.isRunning()) {
						ad.setCallback(this);
						ad.start();
					}
				} else {
					ad.stop();
					ad.setCallback(null);
				}
			}
		}
	}


	@Override
	public void invalidateDrawable(Drawable who) {
		informationView.invalidate();
	}


	@Override
	public void scheduleDrawable(Drawable who, Runnable what, long when) {
		handler.postAtTime(what, who, when);
	}


	@Override
	public void unscheduleDrawable(Drawable who, Runnable what) {
		handler.removeCallbacks(what);
	}
	
}
